//
//  AddModuleCell.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/11/13.
//  Copyright © 2016年 KKING. All rights reserved.
//
#import "ConstValues.h"
#import "AddModuleAlreadyAddedCell.h"
#import "AddModuleAlreadyAddedCollectionViewCell.h"
@interface AddModuleAlreadyAddedCell()<UICollectionViewDataSource,UICollectionViewDelegate,AddModuleAlreadyAddedCollectionViewCellDelegate>
/** View */
@property (weak, nonatomic)  UICollectionView * collectionView;
@end
@implementation AddModuleAlreadyAddedCell
- (void)addObserver
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceivedRefreshAddModuleAlreadyAddedCollectionView:) name:@"refreshAddModuleAlreadyAddedCollectionViewNotification" object:nil];
    
}
- (void)didReceivedRefreshAddModuleAlreadyAddedCollectionView:(NSNotification *)notify
{
    [self.collectionView reloadData];
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)setAlreadyAdded:(NSMutableArray<DBModel *> *)alreadyAdded{
    _alreadyAdded = alreadyAdded;
    
    
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addObserver];
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 12, 0, 12);
        flowLayout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 12-12-12-12) / 3, kItemHeight);
        
        flowLayout.minimumLineSpacing = 10;
        flowLayout.minimumInteritemSpacing = 12;
        
        UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, kItemHeight * 2 + 10) collectionViewLayout:flowLayout];
        self.
        self.collectionView = collectionView;
        CGFloat red = 245;
        CGFloat green = 245;
        CGFloat blue = 245;
        UIColor * color = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
        collectionView.backgroundColor = color;
        collectionView.dataSource = self;
        collectionView.delegate = self;
        [collectionView registerClass:[AddModuleAlreadyAddedCollectionViewCell class] forCellWithReuseIdentifier:@"AddModuleAlreadyAddedCollectionViewCell"];
//        [collectionView registerNib:[UINib nibWithNibName:@"AddModuleAlreadyAddedCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([AddModuleAlreadyAddedCollectionViewCell class])];
        [self.contentView addSubview:collectionView];
    }
    return self;
}
#pragma mark - <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.alreadyAdded.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    AddModuleAlreadyAddedCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AddModuleAlreadyAddedCollectionViewCell" forIndexPath:indexPath];
    cell.delegate = self;
//    cell.contentView.backgroundColor = indexPath.row % 2 == 0 ? [UIColor colorWithRed:247 / 255.0 green:206 / 255.0 blue:166 / 255.0 alpha:1.0] : [UIColor colorWithRed:242 / 255.0 green:154 / 255.0 blue:76 / 255.0 alpha:1.0];
    cell.model = self.alreadyAdded[indexPath.item];
    cell.alreadyAdded = self.alreadyAdded;
    cell.notAdded = self.notAdded;
    return cell;
}
- (void)refreshCollectionView
{
    [self.collectionView reloadData];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshAddModuleNotAddedCollectionViewNotification" object:nil userInfo:nil];
    
}
@end
